package jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 将教语文的老师工资涨500.
 * 测试事务的提交与回滚
 */
public class Test {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection();){
            connection.setAutoCommit(false);

            String sql = "UPDATE teacher " +
                         "SET salary = salary+500 " +
                         "WHERE subject_id=(SELECT id FROM subject WHERE name='语文')";

            Statement statement = connection.createStatement();
            int num = statement.executeUpdate(sql);
            System.out.println(num>0?"涨工资成功":"涨工资失败");

//            connection.commit();
//            System.out.println("提交了事务");

            connection.rollback();
            System.out.println("事务回滚了");

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}





